<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.9.1"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>AIfES 2: Tutorial inference F32</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtreedata.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
    extensions: ["tex2jax.js"],
    jax: ["input/TeX","output/HTML-CSS"],
});
</script>
<script type="text/javascript" async="async" src="https://cdn.jsdelivr.net/npm/mathjax@2/MathJax.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectlogo"><img alt="Logo" src="AIfES_logo_small.png"/></td>
  <td id="projectalign" style="padding-left: 0.5em;">
   <div id="projectname">AIfES 2
   &#160;<span id="projectnumber">2.0.0</span>
   </div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.9.1 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
var searchBox = new SearchBox("searchBox", "search",false,'Search','.html');
/* @license-end */
</script>
<script type="text/javascript" src="menudata.js"></script>
<script type="text/javascript" src="menu.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
$(function() {
  initMenu('',true,false,'search.php','Search');
  $(document).ready(function() { init_search(); });
});
/* @license-end */</script>
<div id="main-nav"></div>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
      <div id="nav-sync" class="sync"></div>
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
$(document).ready(function(){initNavTree('_tutorial_inference_f32.html',''); initResizable(); });
/* @license-end */
</script>
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div class="PageDoc"><div class="header">
  <div class="headertitle">
<div class="title">Tutorial inference F32 </div>  </div>
</div><!--header-->
<div class="contents">
<div class="textblock"><p><a class="anchor" id="md__builds_ims_esa_aifes_2_doc_md_03_tutorial_inference"></a> This tutorial should explain how the different components of AIfES 2 work together to perform an inference (/ prediction / forward pass) on a simple Feed-Forward Neural Network (FNN) or Multi Layer Perceptron (MLP). It is assumed, that the trained weights are already available or will be calculated with external tools on a PC. If you want to train the neural network with AIfES, switch to the <a class="el" href="_tutorial_training_f32.html">training tutorial</a>.</p>
<h1><a class="anchor" id="autotoc_md6"></a>
Example</h1>
<p>As an example, we take a robot with two powered wheels and a RGB color sensor that should follow a black line on a white paper. To fulfill the task, we map the color sensor values with an FNN directly to the control commands for the two wheel-motors of the robot. The inputs for the FNN are the RGB color values scaled to the interval [0, 1] and the outputs are either "on" (1) or "off" (0) to control the motors.</p>
<div class="image">
<img src="example_line_follow_robot.png" alt="" width="600px"/>
</div>
<p>The following cases should be considered:</p><ol type="1">
<li>The sensor points to a black area (RGB = [0, 0, 0]): The robot is too far on the left and should turn on the left wheel-motor while removing power from the right motor.</li>
<li>The sensor points to a white area (RGB = [1, 1, 1]): The robot is too far on the right and should turn on the right wheel-motor while removing power from the left motor.</li>
<li>The sensor points to a red area (RGB = [1, 0, 0]): The robot reached the stop mark and should switch off both motors.</li>
</ol>
<p>The resulting input data of the FNN is then</p>
<table class="markdownTable">
<tr class="markdownTableHead">
<th class="markdownTableHeadNone">R   </th><th class="markdownTableHeadNone">G   </th><th class="markdownTableHeadNone">B    </th></tr>
<tr class="markdownTableRowOdd">
<td class="markdownTableBodyNone">0   </td><td class="markdownTableBodyNone">0   </td><td class="markdownTableBodyNone">0    </td></tr>
<tr class="markdownTableRowEven">
<td class="markdownTableBodyNone">1   </td><td class="markdownTableBodyNone">1   </td><td class="markdownTableBodyNone">1    </td></tr>
<tr class="markdownTableRowOdd">
<td class="markdownTableBodyNone">1   </td><td class="markdownTableBodyNone">0   </td><td class="markdownTableBodyNone">0   </td></tr>
</table>
<p>and the output should be</p>
<table class="markdownTable">
<tr class="markdownTableHead">
<th class="markdownTableHeadNone">left motor   </th><th class="markdownTableHeadNone">right motor    </th></tr>
<tr class="markdownTableRowOdd">
<td class="markdownTableBodyNone">1   </td><td class="markdownTableBodyNone">0    </td></tr>
<tr class="markdownTableRowEven">
<td class="markdownTableBodyNone">0   </td><td class="markdownTableBodyNone">1    </td></tr>
<tr class="markdownTableRowOdd">
<td class="markdownTableBodyNone">0   </td><td class="markdownTableBodyNone">0   </td></tr>
</table>
<h1><a class="anchor" id="autotoc_md7"></a>
Design the neural network</h1>
<p>To set up the FNN in AIfES 2, we need to design the structure of the neural network. It needs three inputs for the RGB color values and two outputs for the two motors. Because the task is rather easy, we use just one hidden layer with three neurons.</p>
<div class="image">
<img src="example_line_follow_abstract_model.png" alt="" width="800px"/>
</div>
<p>To create the network in AIfES, it must be divided into logical layers like the fully-connected (dense) layer and the activation functions. We choose a Leaky ReLU activation for the hidden layer and a Sigmoid activation for the output layer.</p>
<div class="image">
<img src="example_line_follow_model.png" alt="" width="800px"/>
</div>
<h1><a class="anchor" id="autotoc_md8"></a>
Get the pre-trained weights and biases</h1>
<p>To perform an inference you need the trained weights and biases of the model. For example you can train your model with Keras or PyTorch, extract the weights and biases and copy them to your AIfES model. <br  />
 For a dense layer, AIfES expects the weights as a matrix of shape [Inputs x Outputs] and the bias as a matrix of shape [1 x Outputs].</p>
<p>Example model in Keras: </p><div class="fragment"><div class="line">model = Sequential()</div>
<div class="line"> </div>
<div class="line">model.add(Input(shape=(3,)))</div>
<div class="line">model.add(Dense(3))</div>
<div class="line">model.add(LeakyReLU(alpha=0.01))</div>
<div class="line">model.add(Dense(2))</div>
<div class="line">model.add(Activation(<span class="stringliteral">&#39;sigmoid&#39;</span>))</div>
</div><!-- fragment --><p>Example model in PyTorch: </p><div class="fragment"><div class="line"><span class="keyword">class </span>Net(nn.Module):</div>
<div class="line"> </div>
<div class="line">    <span class="keyword">def </span>__init__(self):</div>
<div class="line">        super(Net, self).__init__()</div>
<div class="line">        </div>
<div class="line">        self.dense_layer_1 = nn.Linear(3, 3)</div>
<div class="line">        self.leaky_relu_layer = nn.LeakyReLU(0.01)</div>
<div class="line">        self.dense_layer_2 = nn.Linear(3, 2)</div>
<div class="line">        self.sigmoid_layer = nn.Sigmoid()</div>
<div class="line"> </div>
<div class="line">    <span class="keyword">def </span>forward(self, x):</div>
<div class="line">        x = self.dense_layer_1(x)</div>
<div class="line">        x = self.leaky_relu_layer(x)</div>
<div class="line">        x = self.dense_layer_2(x)</div>
<div class="line">        x = self.sigmoid_layer(x)</div>
<div class="line">        <span class="keywordflow">return</span> x</div>
</div><!-- fragment --><p>Our example weights and biases for the two dense layers after training are: </p><p class="formulaDsp">
\[ w_1 = \left( \begin{array}{c} 3.64540 &amp; -3.60981 &amp; 1.57631 \\ -2.98952 &amp; -1.91465 &amp; 3.06150 \\ -2.76578 &amp; -1.24335 &amp; 0.71257 \end{array}\right) \]
</p>
 <p class="formulaDsp">
\[ b_1 = \left( \begin{array}{c} 0.72655 &amp; 2.67281 &amp; -0.21291 \end{array}\right) \]
</p>
 <p class="formulaDsp">
\[ w_2 = \left( \begin{array}{c} -1.09249 &amp; -2.44526 \\ 3.23528 &amp; -2.88023 \\ -2.51201 &amp; 2.52683 \end{array}\right) \]
</p>
 <p class="formulaDsp">
\[ b_2 = \left( \begin{array}{c} 0.14391 &amp; -1.34459 \end{array}\right) \]
</p>
<h1><a class="anchor" id="autotoc_md9"></a>
Create the neural network in AIfES</h1>
<p>AIfES provides implementations of the layers for different data types that can be optimized for several hardware platforms. An overview of the layers that are available for inference can be seen in the <a class="el" href="index.html#OverviewInference">overview section of the main page</a>. In the overview table you can click on the layer in the first column for a description on how the layer works. To see how to create the layer in code, choose one of the implementations for your used data type and click on the link. <br  />
 In this tutorial we work with the float 32 data type (<a class="el" href="aimath__f32_8h.html">F32 </a>) and use the default implementations (without any hardware specific optimizations) of the layers.</p>
<p>Used layer types:</p><ul>
<li><a class="el" href="ailayer__input_8h.html">Input layer </a></li>
<li><a class="el" href="ailayer__dense_8h.html">Dense layer </a></li>
<li><a class="el" href="ailayer__leaky__relu_8h.html">Leaky ReLU layer </a></li>
<li><a class="el" href="ailayer__sigmoid_8h.html">Sigmoid layer </a></li>
</ul>
<p>Used implementations:</p><ul>
<li><a class="el" href="ailayer__input__default_8h.html#af7bb369ff05cf9cca9fda5765a951c79" title="Initializes and connect an Input layer  with the F32  default implementation.">ailayer_input_f32_default()</a></li>
<li><a class="el" href="ailayer__dense__default_8h.html#a5f58ad071502178879dccb0acab8b74b" title="Initializes and connect a Dense layer  with the F32  default implementation.">ailayer_dense_f32_default()</a></li>
<li><a class="el" href="ailayer__leaky__relu__default_8h.html#aecb15e9008d56b41a8370d9f38cde60c" title="Initializes and connect a Leaky ReLU layer  with the F32  default implementation.">ailayer_leaky_relu_f32_default()</a></li>
<li><a class="el" href="ailayer__sigmoid__default_8h.html#a1ec45b121e81b85e2109f0072d46f602" title="Initializes and connect a Sigmoid layer  with the F32  default implementation.">ailayer_sigmoid_f32_default()</a></li>
</ul>
<h2><a class="anchor" id="autotoc_md10"></a>
Declaration and configuration of the layers</h2>
<p>For every layer we need to create a variable of the specific layer type and configure it for our needs. See the documentation of the data type and hardware specific implementations (for example <a class="el" href="ailayer__dense__default_8h.html#a5f58ad071502178879dccb0acab8b74b" title="Initializes and connect a Dense layer  with the F32  default implementation.">ailayer_dense_f32_default()</a>) for code examples on how to configure the layers.</p>
<p>Our designed network can be declared with the following code. </p><div class="fragment"><div class="line"><span class="comment">// The main model structure that holds the whole neural network</span></div>
<div class="line"><a class="code" href="structaimodel.html">aimodel_t</a> model;</div>
<div class="line"> </div>
<div class="line"> </div>
<div class="line"><span class="comment">// The layer structures for F32 data type and their configurations</span></div>
<div class="line">uint16_t input_layer_shape[] = {1, 3};</div>
<div class="line"><a class="code" href="structailayer__input.html">ailayer_input_f32_t</a> input_layer = AILAYER_INPUT_F32(2, input_layer_shape);</div>
<div class="line"> </div>
<div class="line"><span class="keyword">const</span> <span class="keywordtype">float</span> dense_layer_1_weights[3*3] = { 3.64540f, -3.60981f, 1.57631f,</div>
<div class="line">                                          -2.98952f, -1.91465f, 3.06150f,</div>
<div class="line">                                          -2.76578f, -1.24335f, 0.71257f};</div>
<div class="line"><span class="keyword">const</span> <span class="keywordtype">float</span> dense_layer_1_bias[1*3] = {0.72655f, 2.67281f, -0.21291f};</div>
<div class="line"><a class="code" href="structailayer__dense.html">ailayer_dense_f32_t</a> dense_layer_1 = AILAYER_DENSE_F32_M(3, dense_layer_1_weights, dense_layer_1_bias);</div>
<div class="line"> </div>
<div class="line"><a class="code" href="structailayer__leaky__relu__f32.html">ailayer_leaky_relu_f32_t</a> leaky_relu_layer = AILAYER_LEAKY_RELU_F32_M(0.01f);</div>
<div class="line"> </div>
<div class="line"><span class="keyword">const</span> <span class="keywordtype">float</span> dense_layer_2_weights[3*2] = {-1.09249f, -2.44526f,</div>
<div class="line">                                           3.23528f, -2.88023f,</div>
<div class="line">                                          -2.51201f,  2.52683f};</div>
<div class="line"><span class="keyword">const</span> <span class="keywordtype">float</span> dense_layer_2_bias[1*2] = {0.14391f, -1.34459f};</div>
<div class="line"><a class="code" href="structailayer__dense.html">ailayer_dense_f32_t</a> dense_layer_2 = AILAYER_DENSE_F32_M(2, dense_layer_2_weights, dense_layer_2_bias);</div>
<div class="line"> </div>
<div class="line"><a class="code" href="structailayer__sigmoid.html">ailayer_sigmoid_f32_t</a> sigmoid_layer = AILAYER_SIGMOID_F32_M();</div>
<div class="ttc" id="astructailayer__dense_html"><div class="ttname"><a href="structailayer__dense.html">ailayer_dense</a></div><div class="ttdoc">General Dense layer  structure.</div><div class="ttdef"><b>Definition:</b> ailayer_dense.h:71</div></div>
<div class="ttc" id="astructailayer__input_html"><div class="ttname"><a href="structailayer__input.html">ailayer_input</a></div><div class="ttdoc">General Input layer  structure.</div><div class="ttdef"><b>Definition:</b> ailayer_input.h:39</div></div>
<div class="ttc" id="astructailayer__leaky__relu__f32_html"><div class="ttname"><a href="structailayer__leaky__relu__f32.html">ailayer_leaky_relu_f32</a></div><div class="ttdoc">Data-type specific Leaky ReLU layer struct for F32 .</div><div class="ttdef"><b>Definition:</b> ailayer_leaky_relu_default.h:51</div></div>
<div class="ttc" id="astructailayer__sigmoid_html"><div class="ttname"><a href="structailayer__sigmoid.html">ailayer_sigmoid</a></div><div class="ttdoc">General Sigmoid layer  struct.</div><div class="ttdef"><b>Definition:</b> ailayer_sigmoid.h:47</div></div>
<div class="ttc" id="astructaimodel_html"><div class="ttname"><a href="structaimodel.html">aimodel</a></div><div class="ttdoc">AIfES artificial neural network model.</div><div class="ttdef"><b>Definition:</b> aifes_core.h:181</div></div>
</div><!-- fragment --><p>We use the initializer macros with the "_M" at the end, because we need to set our parameters (like the weights) to the layers.</p>
<h2><a class="anchor" id="autotoc_md11"></a>
Connection and initialization of the layers</h2>
<p>Afterwards the layers are connected and initialized with the data type and hardware specific implementations </p><div class="fragment"><div class="line"><span class="comment">// Layer pointer to perform the connection</span></div>
<div class="line"><a class="code" href="structailayer.html">ailayer_t</a> *x;</div>
<div class="line"> </div>
<div class="line">model.<a class="code" href="structaimodel.html#a708a94e69112ad215b2b52da2238a711">input_layer</a> = <a class="code" href="ailayer__input__default_8h.html#af7bb369ff05cf9cca9fda5765a951c79">ailayer_input_f32_default</a>(&amp;input_layer);</div>
<div class="line">x = <a class="code" href="ailayer__dense__default_8h.html#a5f58ad071502178879dccb0acab8b74b">ailayer_dense_f32_default</a>(&amp;dense_layer_1, model.<a class="code" href="structaimodel.html#a708a94e69112ad215b2b52da2238a711">input_layer</a>);</div>
<div class="line">x = <a class="code" href="ailayer__leaky__relu__default_8h.html#aecb15e9008d56b41a8370d9f38cde60c">ailayer_leaky_relu_f32_default</a>(&amp;leaky_relu_layer, x);</div>
<div class="line">x = <a class="code" href="ailayer__dense__default_8h.html#a5f58ad071502178879dccb0acab8b74b">ailayer_dense_f32_default</a>(&amp;dense_layer_2, x);</div>
<div class="line">x = <a class="code" href="ailayer__sigmoid__default_8h.html#a1ec45b121e81b85e2109f0072d46f602">ailayer_sigmoid_f32_default</a>(&amp;sigmoid_layer, x);</div>
<div class="line">model.<a class="code" href="structaimodel.html#a7c7ad89e7d15631b3f5893b8f19030ef">output_layer</a> = x;</div>
<div class="line"> </div>
<div class="line"><span class="comment">// Finish the model creation by checking the connections and setting some parameters for further processing</span></div>
<div class="line"><a class="code" href="aialgo__sequential__inference_8h.html#a3fb665166082f1e7a89e23218a105ce8">aialgo_compile_model</a>(&amp;model);</div>
<div class="ttc" id="aaialgo__sequential__inference_8h_html_a3fb665166082f1e7a89e23218a105ce8"><div class="ttname"><a href="aialgo__sequential__inference_8h.html#a3fb665166082f1e7a89e23218a105ce8">aialgo_compile_model</a></div><div class="ttdeci">uint8_t aialgo_compile_model(aimodel_t *model)</div><div class="ttdoc">Initialize the model structure.</div></div>
<div class="ttc" id="aailayer__dense__default_8h_html_a5f58ad071502178879dccb0acab8b74b"><div class="ttname"><a href="ailayer__dense__default_8h.html#a5f58ad071502178879dccb0acab8b74b">ailayer_dense_f32_default</a></div><div class="ttdeci">ailayer_t * ailayer_dense_f32_default(ailayer_dense_f32_t *layer, ailayer_t *input_layer)</div><div class="ttdoc">Initializes and connect a Dense layer  with the F32  default implementation.</div></div>
<div class="ttc" id="aailayer__input__default_8h_html_af7bb369ff05cf9cca9fda5765a951c79"><div class="ttname"><a href="ailayer__input__default_8h.html#af7bb369ff05cf9cca9fda5765a951c79">ailayer_input_f32_default</a></div><div class="ttdeci">ailayer_t * ailayer_input_f32_default(ailayer_input_f32_t *layer)</div><div class="ttdoc">Initializes and connect an Input layer  with the F32  default implementation.</div></div>
<div class="ttc" id="aailayer__leaky__relu__default_8h_html_aecb15e9008d56b41a8370d9f38cde60c"><div class="ttname"><a href="ailayer__leaky__relu__default_8h.html#aecb15e9008d56b41a8370d9f38cde60c">ailayer_leaky_relu_f32_default</a></div><div class="ttdeci">ailayer_t * ailayer_leaky_relu_f32_default(ailayer_leaky_relu_f32_t *layer, ailayer_t *input_layer)</div><div class="ttdoc">Initializes and connect a Leaky ReLU layer  with the F32  default implementation.</div></div>
<div class="ttc" id="aailayer__sigmoid__default_8h_html_a1ec45b121e81b85e2109f0072d46f602"><div class="ttname"><a href="ailayer__sigmoid__default_8h.html#a1ec45b121e81b85e2109f0072d46f602">ailayer_sigmoid_f32_default</a></div><div class="ttdeci">ailayer_t * ailayer_sigmoid_f32_default(ailayer_sigmoid_f32_t *layer, ailayer_t *input_layer)</div><div class="ttdoc">Initializes and connect a Sigmoid layer  with the F32  default implementation.</div></div>
<div class="ttc" id="astructailayer_html"><div class="ttname"><a href="structailayer.html">ailayer</a></div><div class="ttdoc">AIfES layer interface.</div><div class="ttdef"><b>Definition:</b> aifes_core.h:252</div></div>
<div class="ttc" id="astructaimodel_html_a708a94e69112ad215b2b52da2238a711"><div class="ttname"><a href="structaimodel.html#a708a94e69112ad215b2b52da2238a711">aimodel::input_layer</a></div><div class="ttdeci">ailayer_t * input_layer</div><div class="ttdoc">Input layer of the model that gets the input data.</div><div class="ttdef"><b>Definition:</b> aifes_core.h:182</div></div>
<div class="ttc" id="astructaimodel_html_a7c7ad89e7d15631b3f5893b8f19030ef"><div class="ttname"><a href="structaimodel.html#a7c7ad89e7d15631b3f5893b8f19030ef">aimodel::output_layer</a></div><div class="ttdeci">ailayer_t * output_layer</div><div class="ttdoc">Output layer of the model.</div><div class="ttdef"><b>Definition:</b> aifes_core.h:183</div></div>
</div><!-- fragment --><h2><a class="anchor" id="autotoc_md12"></a>
Print the layer structure to the console</h2>
<p>To see the structure of your created model, you can print a model summary to the console </p><div class="fragment"><div class="line">aiprintf(<span class="stringliteral">&quot;\n-------------- Model structure ---------------\n&quot;</span>);</div>
<div class="line"><a class="code" href="aialgo__sequential__inference_8h.html#a3bb5fdb556c51ad8b3043d220f0a1276">aialgo_print_model_structure</a>(&amp;model);</div>
<div class="line">aiprintf(<span class="stringliteral">&quot;----------------------------------------------\n\n&quot;</span>);</div>
<div class="ttc" id="aaialgo__sequential__inference_8h_html_a3bb5fdb556c51ad8b3043d220f0a1276"><div class="ttname"><a href="aialgo__sequential__inference_8h.html#a3bb5fdb556c51ad8b3043d220f0a1276">aialgo_print_model_structure</a></div><div class="ttdeci">void aialgo_print_model_structure(aimodel_t *model)</div><div class="ttdoc">Print the layer structure of the model with the configured parameters.</div></div>
</div><!-- fragment --><h1><a class="anchor" id="autotoc_md13"></a>
Perform the inference</h1>
<h2><a class="anchor" id="autotoc_md14"></a>
Allocate and initialize the working memory</h2>
<p>Because AIfES doesn't allocate any memory on its own, you have to set the memory buffers for the inference manually. This memory is required for example for the intermediate results of the layers. Therefore you can choose fully flexible, where the buffer should be located in memory. To calculate the required amount of memory for the inference, the <a class="el" href="aialgo__sequential__inference_8h.html#a877ce6eee19a9f9bcbdb115d83537e68" title="Calculate the memory requirements for intermediate results of an inference.">aialgo_sizeof_inference_memory()</a> function can be used. <br  />
 With <a class="el" href="aialgo__sequential__inference_8h.html#a7cbfac6a46c02107d19af7c8f6e5469a" title="Assign the memory for intermediate results of an inference to the model.">aialgo_schedule_inference_memory()</a> a memory block of the required size can be distributed and scheduled (memory regions might be shared over time) to the model.</p>
<p>A dynamic allocation of the memory using malloc could look like the following: </p><div class="fragment"><div class="line">uint32_t inference_memory_size = <a class="code" href="aialgo__sequential__inference_8h.html#a877ce6eee19a9f9bcbdb115d83537e68">aialgo_sizeof_inference_memory</a>(&amp;model);</div>
<div class="line"><span class="keywordtype">void</span> *inference_memory = malloc(inference_memory_size);</div>
<div class="line"> </div>
<div class="line"><span class="comment">// Schedule the memory to the model</span></div>
<div class="line"><a class="code" href="aialgo__sequential__inference_8h.html#a7cbfac6a46c02107d19af7c8f6e5469a">aialgo_schedule_inference_memory</a>(&amp;model, inference_memory, inference_memory_size);</div>
<div class="ttc" id="aaialgo__sequential__inference_8h_html_a7cbfac6a46c02107d19af7c8f6e5469a"><div class="ttname"><a href="aialgo__sequential__inference_8h.html#a7cbfac6a46c02107d19af7c8f6e5469a">aialgo_schedule_inference_memory</a></div><div class="ttdeci">uint8_t aialgo_schedule_inference_memory(aimodel_t *model, void *memory_ptr, uint32_t memory_size)</div><div class="ttdoc">Assign the memory for intermediate results of an inference to the model.</div></div>
<div class="ttc" id="aaialgo__sequential__inference_8h_html_a877ce6eee19a9f9bcbdb115d83537e68"><div class="ttname"><a href="aialgo__sequential__inference_8h.html#a877ce6eee19a9f9bcbdb115d83537e68">aialgo_sizeof_inference_memory</a></div><div class="ttdeci">uint32_t aialgo_sizeof_inference_memory(aimodel_t *model)</div><div class="ttdoc">Calculate the memory requirements for intermediate results of an inference.</div></div>
</div><!-- fragment --><p>You could also pre-define a memory buffer if you know the size in advance, for example </p><div class="fragment"><div class="line"><span class="keyword">const</span> uint32_t inference_memory_size = 24;</div>
<div class="line"><span class="keywordtype">char</span> inference_memory[inference_memory_size];</div>
<div class="line"> </div>
<div class="line">...</div>
<div class="line"> </div>
<div class="line"><span class="comment">// Schedule the memory to the model</span></div>
<div class="line">aialgo_schedule_inference_memory(&amp;model, inference_memory, inference_memory_size);</div>
</div><!-- fragment --><h2><a class="anchor" id="autotoc_md15"></a>
Run the inference</h2>
<p>To perform the inference, the input data must be packed in a tensor to be processed by AIfES. A tensor in AIfES is just a N-dimensional array that is used to hold the data values in a structured way. To do this in the example, create a 2D tensor of the used data type. The shape describes the size of the dimensions of the tensor. The first dimension (the rows) is the batch dimension, i.e. the dimension of the different input samples. If you process just one sample at a time, this dimension is 1. The second dimension equals the inputs of the neural network.</p>
<div class="fragment"><div class="line">uint16_t in_shape[2] = {1, 3};</div>
<div class="line"><span class="keywordtype">float</span> in_data[1*3] = {1.0f, 0.0f, 0.0f};</div>
<div class="line"><a class="code" href="structaitensor.html">aitensor_t</a> in = AITENSOR_2D_F32(in_shape, in_data);</div>
<div class="ttc" id="astructaitensor_html"><div class="ttname"><a href="structaitensor.html">aitensor</a></div><div class="ttdoc">A tensor in AIfES.</div><div class="ttdef"><b>Definition:</b> aifes_math.h:89</div></div>
</div><!-- fragment --><p>Now everything is ready to perform the actual inference. For this you can use the function <a class="el" href="aialgo__sequential__inference_8h.html#a295cb2bb6c3fefc478042f66e067e090" title="Perform an inference on the model / Run the model.">aialgo_inference_model()</a>.</p>
<div class="fragment"><div class="line"><span class="comment">// Create an empty tensor for the inference results</span></div>
<div class="line">uint16_t out_shape[2] = {1, 2};</div>
<div class="line"><span class="keywordtype">float</span> out_data[1*2];</div>
<div class="line"><a class="code" href="structaitensor.html">aitensor_t</a> out = AITENSOR_2D_F32(out_shape, out_data);</div>
<div class="line"> </div>
<div class="line"><a class="code" href="aialgo__sequential__inference_8h.html#a295cb2bb6c3fefc478042f66e067e090">aialgo_inference_model</a>(&amp;model, &amp;in, &amp;out);</div>
<div class="ttc" id="aaialgo__sequential__inference_8h_html_a295cb2bb6c3fefc478042f66e067e090"><div class="ttname"><a href="aialgo__sequential__inference_8h.html#a295cb2bb6c3fefc478042f66e067e090">aialgo_inference_model</a></div><div class="ttdeci">uint8_t aialgo_inference_model(aimodel_t *model, aitensor_t *input_data, aitensor_t *output_data)</div><div class="ttdoc">Perform an inference on the model / Run the model.</div></div>
</div><!-- fragment --><p>Alternative you can also do the inference without creating an empty tensor for the result with the function <a class="el" href="aialgo__sequential__inference_8h.html#a4655caab3051cc837312c286fbe4789a" title="Perform a forward pass on the model.">aialgo_forward_model()</a>. The results of this function are stored in the inference memory. If you want to perform another inference or delete the inference memory, you have to save the results first to another tensor / array. Otherwise you will loose the data. </p><div class="fragment"><div class="line"><a class="code" href="structaitensor.html">aitensor_t</a> *y = <a class="code" href="aialgo__sequential__inference_8h.html#a4655caab3051cc837312c286fbe4789a">aialgo_forward_model</a>(&amp;model, &amp;in);</div>
<div class="ttc" id="aaialgo__sequential__inference_8h_html_a4655caab3051cc837312c286fbe4789a"><div class="ttname"><a href="aialgo__sequential__inference_8h.html#a4655caab3051cc837312c286fbe4789a">aialgo_forward_model</a></div><div class="ttdeci">aitensor_t * aialgo_forward_model(aimodel_t *model, aitensor_t *input_data)</div><div class="ttdoc">Perform a forward pass on the model.</div></div>
</div><!-- fragment --><p>Afterwards you can print the results to the console for debugging purposes: </p><div class="fragment"><div class="line">aiprintf(<span class="stringliteral">&quot;input:\n&quot;</span>);</div>
<div class="line"><a class="code" href="aimath__basic_8h.html#ab10c8d06990943806f0be8fcc6af03fc">print_aitensor</a>(&amp;in);</div>
<div class="line">aiprintf(<span class="stringliteral">&quot;NN output:\n&quot;</span>);</div>
<div class="line"><a class="code" href="aimath__basic_8h.html#ab10c8d06990943806f0be8fcc6af03fc">print_aitensor</a>(&amp;out);</div>
<div class="ttc" id="aaimath__basic_8h_html_ab10c8d06990943806f0be8fcc6af03fc"><div class="ttname"><a href="aimath__basic_8h.html#ab10c8d06990943806f0be8fcc6af03fc">print_aitensor</a></div><div class="ttdeci">void print_aitensor(const aitensor_t *tensor)</div><div class="ttdoc">Printing a tensor to console.</div></div>
</div><!-- fragment --> </div></div><!-- contents -->
</div><!-- PageDoc -->
</div><!-- doc-content -->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
  <ul>
    <li class="footer">Generated by <a href="https://www.doxygen.org/index.html"><img class="footer" src="doxygen.svg" width="104" height="31" alt="doxygen"/></a> 1.9.1 </li>
  </ul>
</div>
</body>
</html>
